﻿#if ASTORIA_CLIENT
namespace System.Data.Services.Client.Providers
#else
namespace System.Data.Services.Providers
#endif
{
    #region Namespaces
    using System.Collections.Generic;
    using System.Diagnostics;
    using Microsoft.Data.Edm;
    using Microsoft.Data.Edm.Library;
    using Microsoft.Data.Edm.Values;
    using Microsoft.Data.OData.Evaluation;
    #endregion Namespaces

    internal class EdmEnumTypeWithMembers : EdmEnumType, IEdmEnumType
    {
        internal EdmEnumTypeWithMembers(
            Type type,
            string namespaceName,
            string name,
            IEdmPrimitiveType underlyingType,
            bool isFlags)
            : base(namespaceName, name, underlyingType, isFlags)
        {
            Debug.Assert(type.IsEnum != false, "Type must is enum");

            string[] names = Enum.GetNames(type);
            Array values = Enum.GetValues(type);

            Type baseType = Enum.GetUnderlyingType(type);

            for (int i = 0; i < names.Length; i++)
            {
                object value = Convert.ChangeType(values.GetValue(i), baseType, null);
                IEdmPrimitiveValue pValue = EdmValueUtils.ConvertPrimitiveValue(value, null) as IEdmPrimitiveValue;
                this.AddMember(names[i], pValue);
            }
        }
    }
}
